package org.xxpay.pay.service.impl;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.domain.api.msg.AgentPayBalanceRetMsg;
import org.xxpay.core.common.domain.api.msg.QueryRetMsg;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.entity.TransOrder;
import org.xxpay.core.service.IXxPayTransService;
import org.xxpay.pay.channel.TransInterface;
import org.xxpay.pay.service.CommonService;
import org.xxpay.pay.util.SpringUtil;

/**
 * @author: dingzhiwei
 * @date: 2018/5/29
 * @description:
 */
@Service(interfaceName = "org.xxpay.core.service.IXxPayTransService", version = Constant.XXPAY_SERVICE_VERSION, retries = Constant.DUBBO_RETRIES)
public class XxPayTransServiceImpl implements IXxPayTransService {

    private static final MyLog _log = MyLog.getLog(XxPayTransServiceImpl.class);

    @Autowired
    private CommonService commonService;

    @Override
    public AgentPayBalanceRetMsg queryBalance(String channelType, String payParam) {
        try{
            TransInterface transInterface = (TransInterface) SpringUtil.getBean(channelType + "TransService");
            return transInterface.balance(payParam);
        }catch (BeansException e) {
            _log.warn("不支持的查询余额渠道.channelType={}", channelType);
            return AgentPayBalanceRetMsg.fail();
        }
    }

    @Override
    public QueryRetMsg queryTrans(String transOrderId) {
        String channelType= "";
        try{
            TransOrder transOrder = commonService.transOrderService.findByTransOrderId(transOrderId);
            if(transOrder == null) return null;
            channelType = transOrder.getChannelType();
            TransInterface transInterface = (TransInterface) SpringUtil.getBean(channelType + "TransService");
            return transInterface.query(transOrder);
        }catch (BeansException e) {
            _log.warn("不支持的代付查询渠道.channelType={}", channelType);
        }
        return null;
    }

}
